﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Diagnostics;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CaseReview.Model;
using CaseReview.SQLServerDAL;
using CaseReview.Utility;
using Utility;

public partial class CaseManagement_dispatch : System.Web.UI.Page
{

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindData();
        }

       
    }

    public void BindData() {
        string sql =
            "select [caseId],[Title],[Age],[Labor],[Idle],[Owner],[Severity],[Phase],[ServiceLevel],[Type],[CountOfOwnership],[Status],[date] from [CaseReview].[dbo].[CaseInfo] where Status = 0";
        DataTable dataTable = ExecuteDt(sql);
        GridView1.DataSource = dataTable;
        GridView1.DataKeyNames = new string[]{"CaseId"};
        GridView1.DataBind();
    }

    public void BindDispatchData() {
        
    }


    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (((DropDownList)e.Row.FindControl("DropDownList")) != null)
        {
            DropDownList dropDownList = (DropDownList)e.Row.FindControl("DropDownList");
            dropDownList.Items.Clear();
            dropDownList.Items.Add(new ListItem("--Choose--", "0"));
            string sql = "select * from [CaseReview].[dbo].[UserInfo] ";
            DataTable dataTable = ExecuteDt(sql);
            foreach (DataRow dataRow in dataTable.Rows) {
                ListItem listItem= new ListItem(dataRow["Alias"].ToString());
                dropDownList.Items.Add(listItem);
            }
        }
    }
    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e) {
        GridView1.PageIndex = e.NewPageIndex;
        BindData();
    }


    public void btnSendMail_Click(object sender, EventArgs e)
    {   
       //write to database
        DropDownList dropDownList;
        List<string> reviewerList = new List<string>();
        foreach (GridViewRow gridViewRow in GridView1.Rows ) {
            dropDownList = (DropDownList)gridViewRow.FindControl("DropDownList");
            reviewerList.Add(dropDownList.SelectedValue);
        }
     //   _connStr = GetConnString();
        //using (SqlConnection sqlConnection = new SqlConnection(_connStr))
        //{
        //    sqlConnection.Open();
        //    for (int i = 0; i < reviewerList.Count; i++)
        //    {
        //        Dictionary<string, int> temp = (Dictionary<string, int>)Application["Reviewer"];
        //        int id = temp[reviewerList[i]];
        //        string sql = "update [CaseReview].[dbo].[CaseInfo] set Reviewer = id";
        //        SqlCommand sqlCommand = new SqlCommand(sql, sqlConnection);
        //    }
        //    sqlConnection.Close();
        //}
        //set the reviewer
        IList<CaseInfo> caseInfoList = BusinessLogic.GetInstance().GetTotalCaseByStatus(0);
        Dictionary<string, int> temp = (Dictionary<string, int>)Application["Reviewer"];
        string oldCaseId = null;
        for (int i = 0; i < caseInfoList.Count; i++) {   
            caseInfoList[i].Reviewer = temp[reviewerList[i]];
            caseInfoList[i].ReviewerName = reviewerList[i];
            oldCaseId = caseInfoList[i].CaseId;
            BusinessLogic.GetInstance().UpdateCase(caseInfoList[i],oldCaseId);
        }
        //send the email
        SendMail.SendReviewerMail(caseInfoList);

        //set the status
        for (int i = 0; i < caseInfoList.Count; i++) {
            caseInfoList[i].Status = 1;
            oldCaseId = caseInfoList[i].CaseId;
            BusinessLogic.GetInstance().UpdateCase(caseInfoList[i], oldCaseId);
        }
        btnSendMail.Enabled = false;
        Response.Write("<script>alert(\"Successful!\")</script>");
    }

    public static DataTable ExecuteDt(String Sqlstr) {
        String ConnStr = GetSqlConnection(); 
        using (SqlConnection conn = new SqlConnection(ConnStr)) 
        { SqlDataAdapter da = new SqlDataAdapter(Sqlstr, conn);
            DataTable dt = new DataTable(); conn.Open();
            da.Fill(dt); 
            conn.Close();
            return dt; 
        }
    }

    public static DataSet ExecuteDs(String Sqlstr)
    {
        String ConnStr = GetSqlConnection();
        using (SqlConnection conn = new SqlConnection(ConnStr))
        {
            SqlDataAdapter da = new SqlDataAdapter(Sqlstr, conn);
            DataSet ds = new DataSet();
            conn.Open();
            da.Fill(ds);
            conn.Close();
            return ds;
        }
    }
    public static string GetSqlConnection()
    {
        ConnectionStringSettingsCollection connectionStringSettingsCollection =
            System.Web.Configuration.WebConfigurationManager.ConnectionStrings as ConnectionStringSettingsCollection;
        ConnectionStringSettings connectionStringSettings = connectionStringSettingsCollection["dbLocalConnectionString"];
        return connectionStringSettings.ConnectionString;
    }
}